<?php

namespace app\admin\model\game;

use think\Model;


class GameRecord extends Model
{

    

    

    // 表名
    protected $name = 'game_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'game_status_text',
        'start_time_text',
        'update_time_text'
    ];
    

    
    public function getGameStatusList()
    {
        return ['not_started' => __('Game_status not_started'), 'in_progress' => __('Game_status in_progress'), 'finished' => __('Game_status finished')];
    }


    public function getGameStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['game_status'] ?? '');
        $list = $this->getGameStatusList();
        return $list[$value] ?? '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['start_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['update_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
